package javafx_20092010_reeks2.samsegers.Screencapturing.FocusPack;

import java.awt.*;
import java.awt.event.*;
import javafx.lang.FX;
import javax.swing.*;

/**
 * De bedoeling is da het schermpje on top of the screen blijft..
 */
public class FocusRequester extends MouseAdapter
        implements AWTEventListener, MouseMotionListener {

    private JFrame frame = null;

    private Point startPoint = null;
    private int startX = 0;
    private int startY = 0;

    public FocusRequester() {
        // Listen to ComponentEvent
        Toolkit.getDefaultToolkit().addAWTEventListener(
                this, AWTEvent.COMPONENT_EVENT_MASK);
    }

    public JFrame getFrame() {
        return frame;
    }

    @Override
    public void eventDispatched(AWTEvent event) {

        if(event.getID() == ContainerEvent.COMPONENT_RESIZED) {

            if(event.getSource() instanceof JFrame) {

                // Remove AWT Event Listener
                Toolkit.getDefaultToolkit().removeAWTEventListener(this);

                if(frame==null){// Get instance of Stage - Frame
                    frame = (JFrame) event.getSource();
                    frame.dispose();

                    // Create Dialog instance
                    frame.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent we) {
                            FX.exit();
                        }
                    });

                    // Set dialog attributes
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                    frame.setAlwaysOnTop(true);
                    frame.setVisible(true);
                }
           }
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
        startPoint = frame.getLocationOnScreen();
        startX = me.getX();
        startY = me.getY();
    }

    @Override
    public void mouseDragged(MouseEvent me) {
        startPoint.translate(me.getX() - startX, me.getY() - startY);
        frame.setLocation(startPoint);
    }

    @Override
    public void mouseMoved(MouseEvent me) {

    }
}
